"""
Case Type   : gs_preupgradechk工具
Case Name   : gs_preupgradechk工具A4项对传统主备进程检测
Create At   : 2025/11/17
Owner       : chen
Description :
    1.通过om工具查看数据状态为normal检测A4项
    2.通过om工具查看数据状态为Degraded检测A4项
    3.通过om工具查看数据状态为Unavailable检测A4项
Expect      :
    1.成功
    2.成功
History     :
"""

import unittest
import os
import re
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Tools_Gs_Preupgradechk_TestCase0012(unittest.TestCase):
    def setUp(self):
        LOG.info(f'----{os.path.basename(__file__)} start----')
        self.pri_node = Node('PrimaryDbUser')
        self.sta_node = Node('Standby1DbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.com = Common()
        self.constant = Constant()
        LOG.info("---SetUp：检查数据库状态是否正常---")
        status = primary_sh.get_db_cluster_status()
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_gs_preupgradechk_tools(self):
        text = '----step1: 数据状态为normal; expect: 成功----'
        LOG.info(text)
        cmd = f"gs_preupgradechk -i A4"
        LOG.info(cmd)
        result = self.pri_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd}").result()
        LOG.info(result)
        self.assertIn('Successfully checked process usage status', result)
        self.assertIn('Normal', result)
        node_num = self.com.get_node_num(self.pri_node)
        self.assertEqual(node_num, result.count('[Normal]'))

        text = '----step2: 数据状态为Degraded; expect: 成功----'
        LOG.info(text)
        cmd = r"ps ux|grep 'gaussdb '|grep -v grep|awk '{{print $2}}'|xargs kill -9"
        LOG.info(cmd)
        res = self.sta_node.sh(cmd).result()
        LOG.info(res)
        status = primary_sh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status)
        cmd = f"gs_preupgradechk -i A4"
        LOG.info(cmd)
        result = self.pri_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd}").result()
        LOG.info(result)
        self.assertIn('Successfully checked process usage status', result)
        self.assertIn('Error', result)
        self.assertEqual(node_num, result.count('[Error]'))
        LOG.info('----恢复集群----')
        res = self.sta_sh.start_db_instance(mode='standby')
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, res)


        text = '----step3: 数据状态为Unavailable; expect: 成功----'
        LOG.info(text)
        cmd = f'echo $GAUSSHOME'
        cmd = r"ps ux|grep 'gaussdb '|grep -v grep|awk '{{print $2}}'|xargs kill -9"
        LOG.info(cmd)
        res = self.pri_node.sh(cmd).result()
        LOG.info(res)
        status = primary_sh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Unavailable" in status)
        cmd = f"gs_preupgradechk -i A4"
        LOG.info(cmd)
        result = self.pri_node.sh(f"source {macro.DB_ENV_PATH};"
                                    f"{cmd}").result()
        LOG.info(result)
        self.assertIn('Successfully checked process usage status', result)
        self.assertIn('Error', result)
        self.assertEqual(node_num, result.count('[Error]'))

    def tearDown(self):
        LOG.info('--------清理环境--------')
        res = primary_sh.start_db_instance()
        self.assertIn(self.constant.RESTART_SUCCESS_MSG, res)
        status = primary_sh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f"------{os.path.basename(__file__)} end------")
